home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6222 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  47 lines

  1. Newsgroups: comp.lang.c
  2. Path: baan.nl!news
  3. From: avi@green.baan.nl (Avi Cohen Stuart)
  4. Subject: Re: How do I use abs() on floats ?
  5. Message-ID: <yqp68cy8n7x.fsf@green.baan.nl>
  6. Sender: avi@green.baan.nl
  7. In-Reply-To: timmyd@netcom.com's message of Thu, 22 Feb 1996 20:53:16 GMT
  8. Date: Fri, 23 Feb 1996 08:23:14 GMT
  9. X-Nntp-Posting-Host: green.baan.nl
  10. Reply-To: avi@baan.nl
  11. References: <4ghki0$b44@LNCSEX0003.eu.btco.com> <timmydDn73Ct.C6r@netcom.com>
  12. Organization: Baan Development BV
  13. X-Newsreader: Gnus v5.1
  14.  
  15. In article <timmydDn73Ct.C6r@netcom.com> timmyd@netcom.com (Tim DeBenedictis) writes:
  16.  
  17.    Use fabs() instead of abs().
  18.  
  19.    -Tim DeBenedictis
  20.    timmyd@netcom.com
  21.  
  22.    PS You could always write your own FABS macro, too; might be faster than 
  23.    fabs() but I doubt it:
  24.  
  25.    #define FABS(x) (x>0.0?x:-x)
  26.  
  27. If you use a macro then do it like this:
  28.  
  29.    #define FABS(x) ((x)>0.0?(x):-(x))
  30.  
  31. The extra braces will ensure that when you use an expression in combination
  32. with FABS() that they will be calculated first.
  33.  
  34. Nice example to go wring:
  35.  
  36. a = FABS(b+c); expands to:
  37.  
  38. a = (b+c>0.0?b+c;-b+c);
  39.                  ^^^^--------- Wrong! 
  40. while the braced version:
  41.  
  42. a = ((b+c)>0.0?(b+x):-(b+c));
  43.  
  44. will do what you expect.
  45.  
  46. Avi.
  47.